package com.bitcola.security.activity.biz;

import com.bitcola.security.activity.constant.PrizeConstant;
import com.bitcola.security.activity.feign.IDataServiceFeign;
import com.bitcola.security.activity.mapper.BalanceChangeLogMapper;
import com.bitcola.security.activity.mapper.ColaUserBalanceMapper;
import com.bitcola.security.activity.mapper.RedPacketHelpLogMapper;
import com.bitcola.security.activity.mapper.WithDrawInMapper;
import com.bitcola.security.auth.common.util.EncoderUtil;
import com.bitcola.security.common.constant.BalanceTypeConstants;
import com.bitcola.security.common.constant.FinancialConstant;
import com.bitcola.security.common.util.Snowflake;
import com.bitcola.entity.BalanceChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service
@Transactional
public class ChargeBiz {
    Snowflake snowflake=new Snowflake(4,4);

    @Resource
    IDataServiceFeign dataServiceFeign;
    @Autowired
    BalanceChangeLogMapper balanceChangeLogMapper;
    @Autowired
    RedPacketHelpLogMapper redPacketHelpLogMapper;


    @Autowired
    WithDrawInMapper withDrawInMapper;
    @Autowired
    ColaUserBalanceMapper colaUserBalanceMapper;

    @Autowired
    BalanceChangeBiz balanceChangeBiz;


    @Transactional
    public void chargePrize(String userId, BigDecimal amount,BigDecimal actor)
    {

        amount=amount.multiply(actor);
        BalanceChange balanceChange=new BalanceChange();
        balanceChange.setId(snowflake.nextIdStr());
        balanceChange.setAmount(amount);
        balanceChange.setSource(BalanceTypeConstants.prize_balance_frozen);
        balanceChange.setType(FinancialConstant.ACTIVITY_REWARD);
        balanceChange.setUserId(userId);
        balanceChange.setCreateTime(new Date());
        balanceChange.setDescription(PrizeConstant.CHARGE_PRIZE);
        balanceChangeLogMapper.insert(balanceChange);
        colaUserBalanceMapper.changeUserBanlance(userId,"USDT",BigDecimal.ZERO,BigDecimal.ZERO,amount,BigDecimal.ZERO,BigDecimal.ZERO, BigDecimal.ZERO,EncoderUtil.BALANCE_KEY);

    }




    @Transactional
    public void chargeInvitorPrize(String userId)
    {
//        var user= dataServiceFeign.findUserInvitor(userId);
//        if(user!=null) {
//            Example example = new Example(ColaMeBalanceWithdrawin.class);
//            example.createCriteria().andEqualTo("userId", user.getId()).andEqualTo("type", "Deposit");
//            var count=withDrawInMapper.selectCountByExample(example);
//            if(count<=1){
//
//
//                balanceChangeBiz.createChange(user.getId(),BalanceTypeConstants.prize_balance_frozen,PrizeConstant.INVITOR_CHARGE_PRIZE,BigDecimal.valueOf(40),FinancialConstant.ACTIVITY_REWARD,"USDT");
//                balanceChangeBiz.createChange(user.getId(),BalanceTypeConstants.BALANCE,null,BigDecimal.valueOf(1.5),FinancialConstant.UNLOCKIN,"USDT");
//                balanceChangeBiz.createChange(user.getId(),BalanceTypeConstants.prize_balance_frozen,null,BigDecimal.valueOf(1.5).negate(),FinancialConstant.UNLOCKOUT,"USDT");
//
//                colaUserBalanceMapper.changeUserBanlance(user.getId(),"USDT",BigDecimal.valueOf(1.5),BigDecimal.ZERO,BigDecimal.valueOf(38.5),BigDecimal.valueOf(1.5),BigDecimal.ZERO, BigDecimal.ZERO,EncoderUtil.BALANCE_KEY);
//
//                //新用户
//
//            }
//
//        }



    }






}
